import axios from 'axios'
import qs from 'qs'
// const Axios = axios.create({
//   baseURL: '/', // 因为我本地做了反向代理
//   timeout: 10000,
//   responseType: 'json',
//   withCredentials: true, // 是否允许带cookie这些
//   headers: {
//     'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8'
//   }
// })
axios.defaults.baseURL = process.env.NODE_ENV === 'production' ? 'https://easy-mock.com/mock/5c4ffde97256a11ea14420f1/ecwalk' : '/ecwalk'
axios.defaults.headers = {
  'X-Requested-With': 'XMLHttpRequest'
}
axios.defaults.timeout = 10000

axios.interceptors.request.use(config => {
  return config
}, err => {
  return Promise.reject(err)
})

axios.interceptors.response.use(response => {
  return response
}, err => {
  if (err && err.response) {
    switch (err.response.status) {
      case 400:
        err.message = '错误请求'
        break
      default:
        err.message = `连接错误${err.response.status}`
    }
  } else {
    err.message = '连接到服务器失败'
  }
  console.log(err.message)
  return Promise.resolve(err.response)
})

export const get = (url, param) => {
  return new Promise((resolve, reject) => {
    axios({
      method: 'get',
      url,
      params: param
    }).then(res => {
      resolve(res)
    })
  })
}

export const post = (url, param) => {
  return new Promise((resolve, reject) => {
    axios({
      method: 'post',
      url,
      data: qs.stringify(param)
    }).then(res => {
      resolve(res)
    })
  })
}
